package com.ryhx.ssm.controller;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.ejb.Startup;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ryhx.ssm.domain.UserT;
import com.ryhx.ssm.service.IUserService;
import com.ryhx.ssm.utils.RadomUtil;

@SuppressWarnings("all")
@Controller
@RequestMapping("/user")
public class UserController {


	private static Logger logger=Logger.getLogger(UserController.class);
	
	@Autowired
	private IUserService userService;
	
	@RequestMapping("/showUser/{userId}")
	public String showUser(@PathVariable Integer userId,HttpServletRequest request ,Model model){
		logger.info("当前查询的userId= "+userId);
		UserT user = userService.getUserById(userId);
		if(user!=null){
			model.addAttribute("user", user);
		}else{
			user=new UserT();
			user.setUserName("你是猴子请来的逗比么！");
			model.addAttribute("user", user);
		}
		return "admin/user/showUser";
	}
	@RequestMapping("/allUser")
	public String userList(HttpServletRequest request ,Model model){
		logger.info("查询所有用户 》》》》》》》》》》》》》》》》》》》》》》");
		String startPage=request.getParameter("page");
		if(startPage==null||startPage.equals("")){
			PageHelper.startPage(1, 30);
		}else{
			PageHelper.startPage(Integer.parseInt(startPage), 30);
		}
		List<UserT> list = userService.getUserList();	
		if(list!=null&&list.size()>0){
			logger.info("当前查到有 "+list.size()+" 个用户！");
			PageInfo<UserT> p = new PageInfo<>(list);
			model.addAttribute("userlist", list);
			model.addAttribute("p", p);
			System.out.println("总页数：" + p.getPages());
			System.out.println("总记录数：" + p.getTotal());
			System.out.println("每页的记录数：" + p.getPageSize());
			System.out.println("当前页码：" + p.getPageNum());
			System.out.println("上一页：" + p.getPrePage());
			System.out.println("下一页：" + p.getNextPage());
			
		}else{
			model.addAttribute("userlist", null);
		}
		return "admin/user/userList";
	}
	
	@RequestMapping("/register")
	public String register(HttpServletRequest request ,Model model){
		logger.info("查询所有用户 》》》》》》》》》》》》》》》》》》》》》》");
		return "register";
	}
	@RequestMapping("/login")
	public String login(HttpServletRequest request ,Model model){
		return "login";
	}
	@RequestMapping("/ajaxlogin")
	public void ajaxLogin(HttpServletRequest request ,Model model){
		
	}
	
	private String print(Object object,HttpServletResponse response) throws IOException {
		if(object==null){
			return null;
		}
		String json=JSON.toJSONString(object);
		response.getWriter().write(json);
		return null;
	}
}
